package buttons.view;

import java.awt.Color;

import view.DisplayList;
import processing.core.PApplet;
import processing.core.PVector;

public class V_DoubleSlider extends V_Button
{
  protected float rect1; // offset of location for rect1
  protected float rect2; // offset of location for rect2
  protected float rectWidth = 8;
  protected float r1_ColorOffset = 0;
  protected float r2_ColorOffset = 0;
  protected float r3_ColorOffset = 0;
  
  public V_DoubleSlider(String n, float xWidth, float yHeight){
    super(n, xWidth, yHeight);
    this.setBGColor(new Color(255, 255, 255));
    rect1 = 0.0f;
    rect2 = xSize;
  }
  
  @Override
  public void display(PApplet p)
  {
    if(visible)
    { 
      int tmpColor = p.color(p.red(bgColor)+offset, p.green(bgColor)+offset, p.blue(bgColor)+offset, transp); 
      p.stroke(0,0,0,transp);
      p.fill(tmpColor);    
      p.rect(xLoc, yLoc, xSize, ySize);
      
      p.noStroke();
      p.fill(200-r1_ColorOffset, 200-r1_ColorOffset, 200-r1_ColorOffset, transp-100);
      p.rect(xLoc+rect1, yLoc, rectWidth, ySize);
      p.fill(200-r2_ColorOffset, 200-r2_ColorOffset, 200-r2_ColorOffset, transp-100);
      p.rect(xLoc+rect2-rectWidth, yLoc, rectWidth, ySize);
      
      p.fill(240-r3_ColorOffset, 240-r3_ColorOffset, 240-r3_ColorOffset, transp);
      p.rect(xLoc+rect1+rectWidth, yLoc+1, rect2-rectWidth-(rect1+rectWidth), ySize-1);
      
      p.stroke(0,0,0);
      p.strokeWeight(0.5f);
      p.noFill();
      p.triangle(xLoc+rect2-rectWidth+2.5f, yLoc + ySize/2,       xLoc+rect2-2.5f, yLoc + 2.5f,     xLoc+rect2-2.5f, yLoc+ySize - 2.5f);
      p.triangle(xLoc+rect1+rectWidth-2.5f, yLoc + ySize/2,       xLoc+rect1+2.5f, yLoc + 2.5f,     xLoc+rect1+2.5f, yLoc+ySize - 2.5f);
      /*// draw a grabby shape on the double slider:
      p.line(xLoc+xSize/2, yLoc+2, xLoc+xSize/2, yLoc+ySize-2);
      p.line(xLoc+xSize/2-2, yLoc+2, xLoc+xSize/2-2, yLoc+ySize-2);
      p.line(xLoc+xSize/2+2, yLoc+2, xLoc+xSize/2+2, yLoc+ySize-2);
      p.line(xLoc+xSize/2+4, yLoc+2, xLoc+xSize/2+4, yLoc+ySize-2);
      p.line(xLoc+xSize/2-4, yLoc+2, xLoc+xSize/2-4, yLoc+ySize-2);
      */
    }
  }
  
  // determines what part of the slider the mouse is over:
  public int containsWhat(float xVal, float yVal)
  {
    // check if mouse is over the first rectangle:
    if (xVal < xLoc + rect1 + rectWidth && xVal > xLoc + rect1
      && yVal < yLoc + ySize && yVal > yLoc)
    {
      return 0;
    }
    // check if mouse is over the second rectangle:
    else if (xVal < xLoc + rect2 && xVal > xLoc+rect2-rectWidth
      && yVal < yLoc + ySize && yVal > yLoc)
    {
      return 1;
    }
    // check if mouse is over the big rectangle:
    else if (xVal < xLoc + rect2-rectWidth && xVal > xLoc+rect1+rectWidth
      && yVal < yLoc + ySize && yVal > yLoc)
    {
      return 2;
    }
    
    else
    {
      return -1;
    }
  }
  
  public void setColorOffsets(float o1, float o2, float o3)
  {
    r1_ColorOffset = o1;
    r2_ColorOffset = o2;
    r3_ColorOffset = o3;
  }
  
  public void moveRect1(float m)
  {
    rect1 = Math.max(0, Math.min(rect2-(rectWidth*2), m));
  }
  
  public void moveRect2(float m)
  {
    rect2 = Math.max(rect1+(rectWidth*2), Math.min(xSize, m));
  }
  
  public PVector getRects()
  {
    return new PVector(rect1, rect2);
  }
  
  public float getXPercent()
  {
    return rect1/xSize;
  }
  
  public float getYPercent()
  {
    return rect2/xSize;
  }
  
  public PVector getAllPercents()
  {
    return new PVector(rect1/xSize, rect2/xSize);
  }
}
